<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Util\SqlServerUtil;
use App\Util\Common;
use Illuminate\Support\Facades\DB;
use  Illuminate\Database\Query\Builder;
use Illuminate\Support\Facades\Cookie;
use Illuminate\Support\Facades\Session;

class CommonController extends Controller
{
	protected $response = [
			'code' => 200,
			'result' => [],
			'message' => ''
	];
	protected $Customer = [];
	//sql server 配置
	protected $serverConfig = [];
	
	protected $SqlServer = null;
	protected $DB = null;
	
	private $ERPUrl ="http://xun2025.com:7080/api/sms/sendsms";
	private $MODE = "ycbj";
		
	public function __construct(Request $request){
			$DB = $request->get("db") ? $request->get("db") : env('DB_DATABASE');
			$serverConfig = [ "UID" => env("DB_USERNAME"), "PWD" => env("DB_PASSWORD"), "Database" => $DB];
			$sqlCon = sqlsrv_connect(env("DB_HOST"), $serverConfig);
// 			dd($serverConfig);
			//数据库账套选择
			$this->DB = $DB;
			if($sqlCon){
				return $this->SqlServer =$sqlCon;
			}
			return false;
		}
		
	
		public function isAuth($request,$type)
		{
			$cookie = $request->cookie('wms_auth');
			$cdata =json_decode($cookie,true);
			if($cdata['username'] !=='admin'){
				if(!strstr($cdata['list'], $type)){return true;}
			}
			return false;
		}
	
		public function getSalesOrderNo($sitecode,$salesorderno)
		{
			$str ='';
			$k = 'S';
			$str = $sitecode.$k.str_pad($salesorderno, 7,0,STR_PAD_LEFT);
			return $str;
		}
		
		public function getStatus($statusid=1)
		{
			return [
				1=>'Draft',
				99=>'Closed',
				100=>'Released',
				101=>'PR',
				102=>'PR',
				105=>'POLinked',
				112=>'FullShipped',	
			][$statusid];
		}
		
		public function getCurrentTime($time=1)
		{
			$getTime =0;
			date_default_timezone_set('PRC');
			if($time == 1){
				$getTime = date('Y-m-d H:i:s',time());
			}elseif($time == 2){
				$getTime = date('Y-m-d H:i:s',time());
				
			}
			return $getTime;
		}
		
		  
		public function getNumberNO($type = 14001,$val=5)
		{
			$gsdh = '00001';
			switch ($type) {
				case 14001:
					$k = 'WL';
					$sql = "select max(RIGHT(FNumber,$val)) as nums from gz_wuliao   ";
					break;
					
				// 报价单	
				case 30001 :
					$k = 'BJD';
					$sql = "select max(RIGHT(Fbillno,$val)) as nums from gz_Ybaojiadan   ";
					break;
				// 配件单	
				case 40001 :
					$k = 'PJD';
					$sql = "select max(RIGHT(Fbillno,$val)) as nums from gz_Ypeijian   ";
					break;
					
					
				default:
					$k = 'WL';
					$sql = "select max(RIGHT(fbillnogs,$val)) as nums from gz_wuliao  ";
			}
			$gsdhData = SqlServerUtil::execToSql($this->SqlServer, $sql, 2);
			if ($gsdhData) {
				$gsdh = $gsdhData['nums'];
				$gsdh = $gsdh + 1;
			} 
			return $k . str_pad($gsdh, $val, '0', STR_PAD_LEFT);
		}
		
		/**
		 * 获取系统单号
		 * @date: 2019年12月19日 上午10:43:16
		 * @author: yy
		 * @FTranType  --81销售订单
		 */
		function getFBillNo($FTranType){
			switch ($FTranType){
				case 81: 
					$key ='WIN';
					break;
				case 21:
					$key='XOUT';
					break;
				default:
					$key ='WIN';
					break;
			}
			
			$sql ="select FProjectVal from t_billcoderule where FBillTypeID ='{$FTranType}' and FProjectID = 1";
			$res =SqlServerUtil::exec($this->SqlServer, $sql,2);
			$key = $res['FProjectVal']?$res['FProjectVal']:$key;
			
			$sql1 ="select FProjectVal,FLength from t_billcoderule where FBillTypeID ='{$FTranType}' and FProjectID = 3";
			$res1 =SqlServerUtil::exec($this->SqlServer, $sql1,2);
			$val = $res1['FProjectVal']?$res1['FProjectVal']:0;
			$len = $res1['FLength']?$res1['FLength']:6;
			$str = str_pad($val,$len,0,STR_PAD_LEFT);
			$str = $key.$str;
			//更新流水号
			$u_sql ="update t_billcoderule set fprojectval = {$val}+1 where fbilltypeid={$FTranType} and fprojectid=3";
			SqlServerUtil::exec($this->SqlServer, $u_sql);
			return $str;
		}
		
		function GetPeriodEndDateNext($FEndDate, $FNext){
            $sql = "select top {$FNext} fenddate from t_perioddate where FEndDate > '{$FEndDate}' order by fenddate";
            $res = SqlServerUtil::exec($this->SqlServer, $sql,2);
            return $res['fenddate'];
        }

    /**
     * 校验日期格式是否正确
     *
     * @param string $date 日期
     * @param string $formats 需要检验的格式数组
     * @return boolean
     */
        function checkDateIsValid($date, $formats = array("Y-m-d", "Y/m/d")) {
            $unixTime = strtotime($date);
            if (!$unixTime) { //strtotime转换不对，日期格式显然不对。
                return false;
            }
            //校验日期的有效性，只要满足其中一个格式就OK
            foreach ($formats as $format) {
                if (date($format, $unixTime) == $date) {
                    return true;
                }
            }
            return false;
        }
		 
		function log($outfile,$msg){
			if (IS_OPEN_LOG !== false) {
				if ($msg != '') {
					if ($outfile !== false) {
						file_put_contents(dirname(__FILE__)."\\..\\..\\..\\..\\public\\".$outfile.'-errors.txt', date('Y-m-d H:i:s').': '.$msg."\n", FILE_APPEND);
					} else {
						file_put_contents(dirname(__FILE__)."\\..\\..\\..\\..\\public\\errors.txt", date('Y-m-d H:i:s').': '.$msg."\n", FILE_APPEND);
					}
				}
			}
		}
		//权限
		public function  checkaccessright($fapp,$ffrm,$ffunc,$Fname){
			$FObjectType = "";
			$FObjectID = "";
			$FAccessMask = "";
			$FAccessMaskV="";
			$FGroupID="";
			$data =[];
			//获取功能模块id、功能id、权限码
			$sql = "select FObjectType,FObjectID,FAccessMask from t_objectfunc where fapp='{$fapp}' and ffrm='{$ffrm}' and ffunc='{$ffunc}' and findex > 0 and fobjecttype <> 78100";
			$sql = iconv('UTF-8', 'GBK', $sql);
			$res = SqlServerUtil::exec($this->SqlServer, $sql);
			if(empty($res)) return '功能模块参数错误';
			if($res && count($res)>0){
				$FObjectType = $res[0]['FObjectType'];
				$FObjectID = $res[0]['FObjectID'];
				$FAccessMask = $res[0]['FAccessMask'];
			}

			if ($FAccessMask!=""){
				//检测是否系统管理员组用户
				$sql = "select t_User.FUserID from t_Group, t_User where t_Group.FUserID = t_User.FUserID And t_User.FName = '{$Fname}' and t_Group.FGroupID = 1";
				//echo $sql;
				$sql = iconv('UTF-8', 'GBK', $sql);
				$res = SqlServerUtil::exec($this->SqlServer, $sql);
				if($res && count($res)>0){
					return true;
				}

				//查出当前用户当前功能具备的权限值 判断是否与功能权限码匹配
				$sql = "select t_accesscontrol.* from t_accesscontrol,t_User where t_accesscontrol.FUserID = t_User.FUserID And  t_User.FName = '{$Fname}' and FObjectType = '{$FObjectType}' and FObjectID = '{$FObjectID}'";
				$sql = iconv('UTF-8', 'GBK', $sql);
				$res = SqlServerUtil::exec($this->SqlServer, $sql);
				if($res && count($res)>0){
					$FAccessMaskV = $res[0]['FAccessMask'];
					if (($FAccessMaskV & $FAccessMask) == $FAccessMask) { //用户自身具备权限
						return true;
					}
				}
				//查出当前用户所在组的当前功能具备的权限值 判断是否与功能权限码匹配
				$sql = "select FGroupID from t_Group,t_User where t_Group.FUserID = t_User.FUserID and t_User.FName = '{$Fname}'";
				//echo $sql;
				$sql = iconv('UTF-8', 'GBK', $sql);
				$res = SqlServerUtil::exec($this->SqlServer, $sql);
				$FGroupID = isset($res[0]['FGroupID'])?$res[0]['FGroupID']:0;

				$sql = "select * from t_accesscontrol where fuserid='{$FGroupID}' and FObjectType = '{$FObjectType}' and FObjectID = '{$FObjectID}'";
				$sql = iconv('UTF-8', 'GBK', $sql);
				$res = SqlServerUtil::exec($this->SqlServer, $sql);
				if($res && count($res)>0){
					$FAccessMaskV = $res[0]['FAccessMask'];
					if (($FAccessMaskV & $FAccessMask) == $FAccessMask) { //用户自身具备权限
						return true;
					}
				}
			}
			$sql_1 ="select FName from t_ObjectFunc where FObjectType = '{$FObjectType}' and FObjectID = '{$FObjectID}' and FIndex =0";
			$res_1 = SqlServerUtil::exec($this->SqlServer, $sql_1,2);
			$FName1 = $res_1['FName']?$res_1['FName']:'采购入库';
			$sql_2 ="select FName from t_ObjectFunc where FObjectType = '{$FObjectType}' and FObjectID = '{$FObjectID}' and FFunc ='{$ffunc}'";
			$res_2 = SqlServerUtil::exec($this->SqlServer, $sql_2,2);
			$FName2 = $res_2['FName']?$res_2['FName']:'新增';
			return '您没有当前操作权限 ('.$FName1.'-'.$FName2.')';
		}
		//判断用户是否为系统用户
		public function IsSystemUser($sUserName,$FUserID=0){
			$sql = '';
			if($FUserID == 0){
				$sql = "select t_User.FUserID from t_Group, t_User where t_Group.FUserID = t_User.FUserID And t_User.FName = '{$sUserName}' and t_Group.FGroupID = 1";
			}else{
				$sql = "select t_User.FUserID from t_Group, t_User where t_Group.FUserID = t_User.FUserID And t_User.FUserID = '{$FUserID}' and t_Group.FGroupID = 1";
			}
			$sql = iconv('UTF-8', 'GBK', $sql);
			$res = SqlServerUtil::exec($this->SqlServer,$sql,2);
			if(empty($res)){
				return false;
			}
			return true;
		}
		//判断用户是否开启了数据授权
		public function IsDataRight($FUserID,$FitemclassID){
			if(!$this->IsSystemUser("",$FUserID)){
				$sql = "select fitemid from t_userdataright where fitemid in (-1) and fuserid = '{$FUserID} ' and fitemclassid=".$FitemclassID;
				$res = SqlServerUtil::exec($this->SqlServer,$sql);
				if(empty($res)){
					return false;
				}
				return true;
			}
		}
		
		public function getjichuziliao(Request $request){
			$typeid = $request->get('typeid');
			if($typeid == 30){
				//部门名称 30
				$sql_1= "SELECT finterid,fname,ftypeid,fid FROM gz_submessage where FTypeID= 30 order by ftypeid ,fid ";
			}elseif ($typeid ==  23){
				//学历 23
				$sql_1="select finterid,fname,ftypeid,fid from gz_submessage where FTypeID= 23 order by ftypeid ,fid";
			}elseif ($typeid == 29){
				//职务 29
				$sql_1="select finterid,fname,ftypeid,fid from gz_submessage where FTypeID= 29 order by ftypeid ,fid";
			}elseif ($typeid == 102){
				//性别 102
				$sql_1="select finterid,fname,ftypeid,fid from gz_submessage where FTypeID= 102 order by ftypeid ,fid";
			}elseif ($typeid == 26){
				//区域 26
				$sql_1="select finterid,fname from gz_submessage where FTypeID= 26 order by ftypeid ,fid";
			}elseif ($typeid == 200){
				//离职 200
				$sql_1="select finterid,fname from gz_submessage where FTypeID= 200 order by ftypeid ,fid";
			}elseif ($typeid == 301){
				//单位  301
				$sql_1="select finterid,fname from gz_submessage where FTypeID= 301 order by ftypeid ,fid";
			}elseif ($typeid == 302){
				//公司名称  302
				$sql_1="select finterid,fname from gz_submessage where FTypeID= 302 order by ftypeid ,fid";
			}elseif ($typeid == 303){
				//币别  303
				$sql_1="select finterid,fname from gz_submessage where FTypeID= 303 order by ftypeid ,fid";
			}else{
				$sql_1="SELECT finterid ,fname,ftypeid,fid FROM gz_submessage where FTypeID= 30
			UNION all
			select finterid,fname,ftypeid,fid from gz_submessage where FTypeID= 23
			UNION all
			select finterid,fname,ftypeid,fid from gz_submessage where FTypeID= 29
			UNION all
			select finterid,fname,ftypeid,fid from gz_submessage where FTypeID= 102
			UNION all
			select finterid,fname,ftypeid,fid from gz_submessage where FTypeID= 26
			UNION all
			select finterid,fname,ftypeid,fid from gz_submessage where FTypeID= 301
			UNION all
			select finterid,fname,ftypeid,fid from gz_submessage where FTypeID= 302
			UNION all
			select finterid,fname,ftypeid,fid from gz_submessage where FTypeID= 303
			UNION all
			select finterid,fname,ftypeid,fid from gz_submessage where FTypeID= 200 order by ftypeid ,fid ";
				
				$Data = SqlServerUtil::exec($this->SqlServer,$sql_1);
				$temp=['23'=>'fdegree','29'=>'fduty','30'=>'fdepid','102'=>'fgender','26'=>'quyu','200'=>'fdeleted','301'=>'funit','302'=>'fcompany','303'=>'fcurrency'];
				$Data2 =[];
				foreach ($Data as $k =>$v)
				{
					$Data2[$temp[$v['ftypeid']]][] =$v;
					//$Data2['bb'][$temp[$v['ftypeid']]][] =$v;
				}
				
				return $Data2;
			}
			
			$Data = SqlServerUtil::exec($this->SqlServer,$sql_1);
			return $Data;
		}
		
		//日志记录
		public function writeLog( $fmkname='',$methodOper ='', $objectid =0, $fobj,  $opername='',  $finterid = 0 ){
			$arr=[
					'fname' => $fmkname, //模块
					'methodOper'=> $methodOper, //功能
					'objectid'=>$objectid, //功能 代码
					'fobj'=>is_array($fobj)?json_encode($fobj, JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE):$fobj, //详情信息
					
					'opername'=> $opername, //操作人
					'finterid'=> $finterid, //订单内码
					'create_at'=>getCurrentTime(),
					'fip' =>  real_ip(),
			]; 
			$in = SqlServerUtil::sqlsrvCreateTosql($this->SqlServer,'gz_Operlog',$arr);
			return  $in;
		}
		
		/**
		* 获取字段 
		* @date: 2022年11月3日 下午4:06:34
		* @author: juyi
		* @param: variable.
		* @FRepPage  0 -- 序时薄 ，1-单据
		* @FUnControl  1-- 新增字段隐藏 ，0 --不隐藏  ，-1----全部
		* 
		* @return:
		*/
		function getVexTableColumn($FRepKey ,$FRepPage= 0 , $FUnControl = 0 ){
			$FUnControlStr  = '';
			if ($FUnControl == 0) {
				$FUnControlStr=" and ( FUnControl =  {$FUnControl} )";
			} 
			
			$basesql="select * from gz_RepListTitle where FRepPage = '{$FRepPage}' and  FRepKey='{$FRepKey}' and FRepHide =0 {$FUnControlStr} order by FRepIndex ";
 
			$baseData = SqlServerUtil::execToSql($this->SqlServer, $basesql );
			
			if($baseData){
				$baseData = arraykeyToLower($baseData);
			 
				foreach ($baseData as $k=>$v)
				{
					$baseData[$k]['frepfld'] = strtolower($v['frepfld']);
				}
			}
			return $baseData;
		}
		
		/**
		* 文件上传 
		* @date: 2023年3月6日 下午1:52:57
		* @author: juyi
		* @param: variable
		* @return:
		*/
		function getVexTableColumnUpload($FRepKey ,$FuploadFile=0 ,$FRepPage= 0 , $FUnControl = 0 ){
			$FUnControlStr  = '';
			if ($FUnControl == 0) {
				$FUnControlStr=" and ( FUnControl =  {$FUnControl} )";
			}
			$FuploadFileStr ='';
			if ($FuploadFile == 0) {
				$FuploadFileStr=" and ( FuploadFile =  {$FuploadFile} )";
			}
			
			$basesql="select * from gz_RepListTitle where FRepPage = '{$FRepPage}' and  FRepKey='{$FRepKey}' and FRepHide =0 {$FUnControlStr} {$FuploadFileStr} order by FRepIndex ";
			
			$baseData = SqlServerUtil::execToSql($this->SqlServer, $basesql );
			
			if($baseData){
				$baseData = arraykeyToLower($baseData);
				
				foreach ($baseData as $k=>$v)
				{
					$baseData[$k]['frepfld'] = strtolower($v['frepfld']);
				}
			}
			return $baseData;
		}
		
		
		
		/**
		* 获取 单据 数据
		* @date: 2022年12月13日 下午2:23:21
		* @author: juyi
		* @param: variable
		* @return:
		*/
		function getVexBillColumn($FRepKey ,$FHeaderFooter =1 ){
			$FHeaderFooterStr  = '';
			if ($FHeaderFooter >= 0) {
				$FHeaderFooterStr=" and ( FHeaderFooter =  {$FHeaderFooter} )";
			} 
			 
			$basesql="select * from gz_RepBillTitle where  FRepKey='{$FRepKey}' and FRepHide =0 {$FHeaderFooterStr}  order by FRepIndex ";
			$baseData = SqlServerUtil::execToSql($this->SqlServer, $basesql );
			
			if($baseData){
				$baseData = arraykeyToLower($baseData);
				
				foreach ($baseData as $k=>$v)
				{
					$baseData[$k]['frepfld'] = strtolower($v['frepfld']);
				}
			}
			return $baseData;
		}
		
		/**
		* 获取 单据编号
		* @date: 2022年11月17日 下午4:21:36
		* @author: juyi
		*/
		public function getDanHao($FRepKey)
		{
			$basesql="select fkey ,fvalue , flen  from gz_xuliehao where FObjectID = '{$FRepKey}' order by findex ";
			$baseData = SqlServerUtil::execToSql($this->SqlServer, $basesql , 2 );
			
			$u_sql = "update gz_xuliehao set fvalue = fvalue +1 where FObjectID = '{$FRepKey}'  ";
			SqlServerUtil::exec($this->SqlServer, $u_sql); 
			$date = getCurrentTime(9) ;
			$str = $baseData['fkey'].$date. str_pad($baseData['fvalue'], $baseData['flen'],0,STR_PAD_LEFT);
			return $str;
		}
		
		
		/**
		* 获取参数
		* @date: 2022年11月25日 下午5:38:08
		* @author: juyi
		* @param: variable
		* @return:
		*/
		public function getSystemProfile($Fkey =''){
			$FkeyStr = '';
			if(!empty($Fkey) && !is_array($Fkey)){
				$FkeyStr = " and FKey in ('{$Fkey}' )";
			}
			if(is_array($Fkey)){
				$fbillno =join("','",$Fkey);
				$fbillnoStr = "'".$fbillno ."'";
				$FkeyStr = " and FKey in ({$fbillnoStr} ) ";
			}
			$sql ="SELECT FCategory,FKey,FValue,FDescription from gz_SystemProfile where 1=1 {$FkeyStr}";
			$res=SqlServerUtil::exec1($this->SqlServer, strtolower($sql) );
			$result =[];
			if($res){
				foreach ($res as $k=>$v){
					$result[strtolower($v['fkey'])] = $v['fvalue'];
				}
			}
			return $result;
		}
		
		
		/**
		* 船检 设置参数值   -- 未确定
		* @date: 2023年1月5日 下午4:46:53
		* @author: juyi
		* @param: variable
		* @return:
		*/
		public function getchuanjianSet($ftype ,$power){
			$sql = " select ftype,fqujian1,fqujian2,fhuilv,fshoufeilx,fgongshi,fallamount  from gz_YchuanjianfSet   "  ;
			
			$res=SqlServerUtil::exec1($this->SqlServer, strtolower($sql) );
			
			$result =[];
			$value = 0; 
			foreach ($res as $k=>$v){
				if($v['ftype'] == $ftype &&  $v['ftype'] == 'LR'){
					if($power>=$v['fqujian1']  && $power<= $v['fqujian2']){
						$value = $v['fgongshi'] ;
						break;
					}
					
				}
				
				if($v['ftype'] == $ftype &&  $v['ftype'] == 'CCS'){
					$value = $v['fshoufeilx'] ;
					break;
				}
				
				if($v['ftype'] == $ftype &&  $v['ftype'] == 'BV'){
					if($power>=$v['fqujian1']  && $power<= $v['fqujian2']){
						$value = $v['fshoufeilx'] ;
						break;
					}
				}
				
				if($v['ftype'] == $ftype &&  $v['ftype'] == 'DNV'){
					$value = $v['fshoufeilx'] ;
					break;
				}
				
				if($v['ftype'] == $ftype &&  $v['ftype'] == 'ABS'){
					if($power>=$v['fqujian1']  && $power<= $v['fqujian2']){
						$value = $v['fshoufeilx'] ;
						break;
					}
					
				}
			}
			return $value;
		}
		
		
		
		/**
		 * 图纸费    === 
		 * @date: 2022年12月27日 下午2:51:39
		 * @author: juyi
		 * @param: $GLOBALS
		 * @return:
		 * WINGD:第一次判断是否同样的机，有，为0;
		 没有，进行二次判断，判断是不是同样的机型不同的缸数，
		 有，则按照不同缸数收费，
		 无，就按照首台
		 
		 
		 MAN:第一次判断是否同样的机，有，按照不同应用型式；
		 没有，进行二次判断，判断是不是同样的机型不同的缸数，
		 有，则按照不同缸数收费；无，就按照首台。
		 */
		function getTZValue($tzData, $fmodel , $fbrand = 'WINGD'  ){
			$value = 0;
			if(empty($tzData)){
				return $value ;
			}
			$fbrand = strtoupper(str_replace(' ','',$fbrand));
			$yiluru = 0;
			//查询已录入 
			$tmpfmodel = '';
			if($fbrand == 'WINGD'){
				$fmodel_nums  =  strripos($fmodel,'-') ;
				$fmodeltemp = $fmodel;
				//如果没有找到字符串则返回 FALSE
				if($fmodel_nums !== false){
					$fmodeltemp = substr($fmodel,0, $fmodel_nums);
				}
				//双燃料机
				if(stripos( strtoupper($fmodeltemp) , 'DF' ) !== false ||  stripos( strtoupper($fmodeltemp) , 'SCR' ) !== false
						|| stripos( strtoupper($fmodeltemp) , 'EGR' ) !== false || stripos( strtoupper($fmodeltemp) , 'LGIM' ) !== false){
				    //查询已录入
					$sql = " select fbrand,fmodel  from gz_Yyiluf  where fmodel like '{$fmodeltemp}%'  "  ;
				}else{
					$sql = " select fbrand,fmodel  from gz_Yyiluf  where fmodel like '{$fmodeltemp}%'  and ( charindex('DF', fmodel)<0 ) "  ;
				}
				$res=SqlServerUtil::exec1($this->SqlServer, strtolower($sql),2 );
				if(!empty($res)){
					$yiluru = 1 ;
				}
				$tmpfmodel = substr($fmodeltemp ,1 );
				
			}else{
				$xtfmodel = substr($fmodel ,0,4);
				$sql2 = " select fbrand,fmodel from gz_Yyiluf where 1=1	and fmodel like '{$xtfmodel}%' "  ;
				$res1 = SqlServerUtil::exec1($this->SqlServer, strtolower($sql2),2 );
				if(!empty($res1)){
					$yiluru = 1 ;
				}
				//MAN
				$tmpfmodel = substr($fmodel ,1,3);
			}
			 
			//WINGD:第一次判断是否同样的机，有，为0;
			if($fbrand == 'WINGD' && $yiluru == 1){
				return $value ;
			}elseif($fbrand == 'WINGD'){
				//双燃料机
				if(stripos( strtoupper($fmodeltemp) , 'DF' ) !== false ||  stripos( strtoupper($fmodeltemp) , 'SCR' ) !== false
						|| stripos( strtoupper($fmodeltemp) , 'EGR' ) !== false || stripos( strtoupper($fmodeltemp) , 'LGIM' ) !== false){
							//查询已录入
							$sql1 = " select fbrand,fmodel from gz_Yyiluf where fmodel != '{$fmodel}' and fmodel like '%{$tmpfmodel}%'  "  ;
				}else{
					$sql1 = " select fbrand,fmodel from gz_Yyiluf where fmodel != '{$fmodel}' and fmodel like '%{$tmpfmodel}%'  and ( charindex('DF', fmodel)<0 )  "  ;
				}
				$res1 = SqlServerUtil::execTosqlOK($this->SqlServer, strtolower($sql1),2 );
				$gs = 0;
// 				echo $sql1;die;
// 				dd($res1  );
				if(!empty($res1)){
					$gs = 1 ;
				}
				
				if(stripos( strtoupper($fmodel) , 'DF' ) !== false){
					$fsetfname = 'srljixing' ;
				}else{
					$fsetfname = 'ptjixing' ;
				}
				if($gs> 0){
					//进行二次判断，判断是不是同样的机型不同的缸数，
					//有，则按照不同缸数收费，
					$fleixing = '不同缸数' ;
				}else{
					// 无，就按照首台
					$fleixing = '型号首台';
				}
			}
			if(strtoupper($fbrand) == 'MAN'){
				$fsetfname = 'alljixing' ;
				//第一次判断是否同样的机，有，按照不同应用型式；
				//没有，进行二次判断，判断是不是同样的机型不同的缸数，
				//有，则按照不同缸数收费；无，就按照首台。
				$sql2 = " select fbrand,fmodel from gz_Yyiluf where fmodel != '{$fmodel}'
				and fmodel like '%{$tmpfmodel}%' "  ;
				$res1 = SqlServerUtil::exec1($this->SqlServer, strtolower($sql2),2 );
				$gs = 0;
				if(!empty($res1)){
					$gs = 1 ;
				}
				
				if($yiluru >0 ){
					$fleixing = '不同应用型式';
				}else{
					if($gs> 0){
						$fleixing = '不同缸数';
					}else{
						$fleixing = '原设计机型';
					}
				}
			}
			foreach ($tzData as  $k=>$v)
			{
				if($v['fsetfname'] == $fsetfname && $fbrand == strtoupper(str_replace(' ','',$v['fbrand'])) && $fleixing == $v['fleixing']   ){
					$value = $v['fallAmount'] ;
					break;
				}
			}
			 
			return $value  ;
		}
		
		
		
		/*发送短信*/
		public function  genSendSms($fphone , $fcontent){
			$mode = $this->MODE ; //模块
			$getData=[
					'fphone'=> $fphone,
					'mode'=> $mode,
					'param1'=> $fcontent,
			];
			$sep ='?';
			$getDataTemp = http_build_query($getData) ;
			$send = $this->ERPUrl.$sep.$getDataTemp  ;
			$result = httpRequest($send, 'get');
			$result = json_decode($result,true) ;
			if($result['code'] == 200 ){
				return  ['code'=>200,'message'=>$result['message']];
			}
			return  ['code'=>400,'message'=>'失败'];
		}
		
		
		
		/**
		* 经营报价 --确认QR
		* @date: 2023年2月27日 下午5:47:02
		* @author: juyi
		* 
		* WINGD:第一次判断是否同样的机，有，为0;  - 查
		
		 没有，进行二次判断，判断是不是同样的机型不同的缸数， 
		 有，则按照不同缸数收费，
		 无，就按照首台
		 
		 MAN:第一次判断是否同样的机，有，按照不同应用型式； 前4位一样
		 没有，进行二次判断，判断是不是同样的机型不同的缸数， 取数字后 3位判断
		 有，则按照不同缸数收费；无，就按照首台。
		 */
		 
		function getTZValueQR($tzData, $fmodel , $fbrand = 'WINGD' ,$fjybjbillno='' ){
// 			dd( $tzData, $fmodel , $fbrand   ,$fjybjbillno );
			$value = [];
			if(empty($tzData)){
				return $value ;
			}
			
			$fbrand = strtoupper(str_replace(' ','',$fbrand));	
			$yiluru = 0;
			//是否有 不同缸数
			$tmpfmodel = '';
			if($fbrand == 'WINGD'){
				$fmodel_nums  =  strripos($fmodel,'-') ; 
				$fmodeltemp = $fmodel;
				//如果没有找到字符串则返回 FALSE
				if($fmodel_nums !== false){
					$fmodeltemp = substr($fmodel,0, $fmodel_nums);
				}
				//双燃料机
				if(stripos( strtoupper($fmodeltemp) , 'DF' ) !== false ||  stripos( strtoupper($fmodeltemp) , 'SCR' ) !== false 
						|| stripos( strtoupper($fmodeltemp) , 'EGR' ) !== false || stripos( strtoupper($fmodeltemp) , 'LGIM' ) !== false){
					//查询已录入
					$sql = " select fbrand,fmodel  from gz_Yyiluf  where fmodel like '{$fmodeltemp}%'  "  ;
				}else{
					$sql = " select fbrand,fmodel  from gz_Yyiluf  where fmodel like '{$fmodeltemp}%'  and ( charindex('DF', fmodel)<0 ) "  ;
				}
				
				$res=SqlServerUtil::exec1($this->SqlServer, strtolower($sql),2 );
			    
				if(!empty($res)){
					$yiluru = 1 ;
				}
				
				$tmpfmodelXL = substr($fmodel ,1,1);
// 				//X系列    7X40-B  ， 6X52-S2.0
// 				$xilie ='';
// 				if(strtoupper($tmpfmodelXL) == 'X' ){
// 					//stripos($fmodel,$tmpfmodelXL)
// 					$tmpfmodel = substr($fmodel ,1,3);
// 				}elseif(strtoupper($tmpfmodelXL) == 'R'){
// 					//RT-flex
// 					//R系列  RT-flex   7RT-flex50-D
// 					$tmpfmodel = substr($fmodel ,1,9);
// 				}
				$tmpfmodel = substr($fmodeltemp ,1 ); 
				 
			}else{
				$xtfmodel = substr($fmodel ,0,4);
				$sql2 = " select fbrand,fmodel from gz_Yyiluf where 1=1	and fmodel like '{$xtfmodel}%' "  ;
				$res1 = SqlServerUtil::exec1($this->SqlServer, strtolower($sql2),2 );
				if(!empty($res1)){
					$yiluru = 1 ;
				}
				//MAN
				$tmpfmodel = substr($fmodel ,1,3);
				
			}
			
			//WINGD:第一次判断是否同样的机，有，为0;
			if($fbrand == 'WINGD' && $yiluru == 1){
				 
				return $value ;
			}elseif($fbrand == 'WINGD'){
				//双燃料机
				if(stripos( strtoupper($fmodeltemp) , 'DF' ) !== false ||  stripos( strtoupper($fmodeltemp) , 'SCR' ) !== false
						|| stripos( strtoupper($fmodeltemp) , 'EGR' ) !== false || stripos( strtoupper($fmodeltemp) , 'LGIM' ) !== false){
							//查询已录入
					$sql1 = " select fbrand,fmodel from gz_Yyiluf where fmodel != '{$fmodel}' and fmodel like '%{$tmpfmodel}%'  "  ;
							
				}else{
					$sql1 = " select fbrand,fmodel from gz_Yyiluf where fmodel != '{$fmodel}' and fmodel like '%{$tmpfmodel}%'  and ( charindex('DF', fmodel)<0 )  "  ;
				}
				
				$res1 = SqlServerUtil::execTosqlOK($this->SqlServer, strtolower($sql1),2 );
				$gs = 0;
				//dd($res1  );
				if(!empty($res1)){
					$gs = 1 ;
				}
				
				if(stripos( strtoupper($fmodel) , 'DF' ) !== false){
					$fsetfname = 'srljixing' ;
				}else{
					$fsetfname = 'ptjixing' ;
				}
				if($gs> 0){
					//进行二次判断，判断是不是同样的机型不同的缸数，
					//有，则按照不同缸数收费，
					$fleixing = '不同缸数' ;
				}else{
					// 无，就按照首台
					$fleixing = '型号首台';
				}
			}
			
			if($fbrand == 'MAN'){
				$fsetfname = 'alljixing' ;
				//第一次判断是否同样的机，有，按照不同应用型式；   前4位一样
				//没有，进行二次判断，判断是不是同样的机型不同的缸数，   取数字后 3位判断
				//有，则按照不同缸数收费；无，就按照首台。
				$sql2 = " select fbrand,fmodel from gz_Yyiluf where fmodel != '{$fmodel}' and fmodel like '%{$tmpfmodel}%' "  ;
				$res2 = SqlServerUtil::exec1($this->SqlServer,  $sql2 ,2 );
// 				dd($res2 , $sql2);
				$gs = 0;
				if(!empty($res2)){
					$gs = 1 ;
				}
				
				if($yiluru >0 ){
					$fleixing = '不同应用型式';
				}else{
					if($gs> 0){
						$fleixing = '不同缸数';
					}else{
						$fleixing = '原设计机型';
					}
				}
			}
			
			foreach ($tzData as  $k=>&$v)
			{
				$v['fjybjbillno'] = $fjybjbillno ;
				unset($v['fid']);
				unset($v['fcreatetime']);
				if($v['fsetfname'] == $fsetfname && $fbrand == strtoupper($v['fbrand']) && $fleixing == $v['fleixing']   ){
					$value = $v ;
					break;
				}
			}
// 			dd($yiluru ,$fleixing,$value );
			return $value  ;
		}
		
		
		/**
		* 船检CCS
		* @date: 2023年2月27日 下午7:05:31
		* @author: juyi
		* @param: variable
		* @return:
		*/
		function getChuanjianQR($fmodel,$FRepKey,$fjybjbillno =''){
			$DB = columnBiao($FRepKey) ;
			$sql2 = " select *  from {$DB} where fmodel  = '{$fmodel}' and fstatus =1 " ;
			$res1 = SqlServerUtil::exec1($this->SqlServer, strtolower($sql2) ,2 );
			if($res1){
				unset($res1['fid']);
				unset($res1['fcreatetime']);
				$res1['fjybjbillno'] = $fjybjbillno ;
				 
			}
			return $res1;
		}
		
		/**
		* 通过机型 -- QR 
		* 质量成本
		* @date: 2023年2月27日 下午7:48:24
		* @author: juyi
		*/
		function getjixingQR($fmodel,$FRepKey,$fjybjbillno =''){
			$DB = columnBiao($FRepKey) ;
			$sql2 = " select *  from {$DB} where fmodel  = '{$fmodel}' and fstatus =1 " ;
			$res1 = SqlServerUtil::exec1($this->SqlServer, strtolower($sql2) ,2 );
			if($res1){
				unset($res1['fid']);
				unset($res1['fcreatetime']);
				$res1['fjybjbillno'] = $fjybjbillno ;
			}
			return $res1;
		}
		
		/**
		* 文件用途描述
		* @date: 2023年4月3日 下午7:20:02
		* @author: juyi
		*/
		function getICERQR($fegcmodel,$FRepKey,$fjybjbillno =''){
			$DB = columnBiao($FRepKey) ;
			$sql2 = " select *  from {$DB} where fegcmodel  = '{$fegcmodel}' " ;
			$res1 = SqlServerUtil::exec1($this->SqlServer, strtolower($sql2) ,2 );
			if($res1){
				unset($res1['fid']);
				unset($res1['fcreatetime']);
				unset($res1['fcreatetimecg']);
				$res1['fjybjbillno'] = $fjybjbillno ;
			}
			return $res1;
		}
		
		 /**
		 * 生产机型 --QR
		 * @date: 2023年2月27日 下午9:58:02
		 * @author: juyi
		 * @param: variable
		 * $CS -- 参数 
		 * @return:
		 */
		function getProdQR($fmodel,$FRepKey,$fjybjbillno ='', $CS ){
			$DB = columnBiao($FRepKey) ;
			$sql2 = " select *  from {$DB} where fmodel  = '{$fmodel}' and fstatus =1 " ;
			$res1 = SqlServerUtil::exec1($this->SqlServer, strtolower($sql2) ,2 );
			if($res1){
				unset($res1['fid']);
				unset($res1['fcreatetime']);
			 
				$res1['fjybjbillno'] = $fjybjbillno ;
				
				$res1['fsetprodfy'] = $CS['fsetprodfy'] ;
				$res1['fsetprodgzftts'] = $CS['fsetprodgzftts'] ;
				$res1['fsetprodxhdjftts'] = $CS['fsetprodxhdjftts']  ;
				$res1['fsetweiwaif'] = $CS['fsetweiwaif'] ;
				
			}
			return $res1;
		}
		
		/**
		* 业务报价 审核 
		* @date: 2023年3月3日 上午9:23:08
		* @author: juyi
		* @param: variable
		* @return:
		*/
		function getYWBAOJIA($fmodel,$FRepKey,$fjybjbillno =''){
			$DB = columnBiao($FRepKey) ;
			$sql2 = " select *  from {$DB} where  fbillno = '{$fjybjbillno}' " ;
// 			echo $sql2 ;die;
			$res1 = SqlServerUtil::exec1($this->SqlServer,  $sql2 );
			if($res1){
				foreach ($res1 as $k=>$v)
				{
					unset($res1[$k]['fid']);
					unset($res1[$k]['fcreatetime']);
				}
			}
			return $res1;
		}
		
		/**
		* 折旧确认
		* @date: 2023年2月27日 下午8:12:39
		* @author: juyi
		*/
		function getzhejiuQR( $fjybjbillno =''){
			$fzhejiund = getCurrentTime(5);
			$zjData = SqlServerUtil::execToSql($this->SqlServer, "select * from gz_Yzhejiuf where fyear ='{$fzhejiund}' and fstatus = 1 " ,2);
			
			if(!empty($zjData)){
				unset($zjData['fid']);
				unset($zjData['fcreatetime']);
				$zjData['fjybjbillno'] = $fjybjbillno ; 
			}
			return $zjData ;
		}
		
		
		/**
		* 函数用途描述
		* @date: 2023年3月8日 上午10:33:32
		* @author: juyi
		* @param: variable
		* @return:
		*/
		function getQDHTQR($fbillno,$FRepKey,$fbillnoentry ='',$FRepKeySC ){
			$DB = columnBiao($FRepKey) ;
			$sql2 = " select *  from {$DB} where fbillno  = '{$fbillno}' " ;
			$res1 = SqlServerUtil::exec1($this->SqlServer, strtolower($sql2) ,2 );
			if($res1){
				unset($res1['fid']);
				unset($res1['fbrandT']);
				unset($res1['fcreatetime']);
				$res1['fstatus']  = 0;
				$res1['checkName']  = '';
				$res1['fbillnoentry'] = $fbillnoentry ;
				if($FRepKeySC == '34001'){
// 					fisqr	fqrJS	fqrCG	fqrCW	opernameJS	opernameCG	opernameCW
					$res1['fisqr']  = 0;
					$res1['fqrJS']  = '';
					$res1['fqrCG']  = '';
					$res1['fqrCW']  = '';
					$res1['opernameJS']  = '';
					$res1['opernameCG']  = '';
					$res1['opernameCW']  = '';
					unset($res1['fcreattimeJS']);
					unset($res1['fcreattimeCG']);
					unset($res1['fcreattimeCW']);
				}
				if($FRepKeySC == '30010'){
					unset($res1['fcreattimeJS']);
					unset($res1['fcreattimeCG']);
					unset($res1['fcreattimeCW']);
				}
				
			}
			return $res1;
		}
		
		
		
		/**
		* 函数用途描述
		* @date: 2023年3月10日 上午9:28:24
		* @author: juyi
		* @param: variable
		* @return:
		*/
		public function getVxetableConfigData($FRepKey,$opername, $FRepPage =0   ){
		$opernameStr = '';
		$ConData =[];
		$fieldData = [];
		$removeData = [];
		//字段权限 查看
		//判断是否在系统管理员组
		$conSql ="SELECT  ad.username,g.fname  from gz_user_group ug
		left join  gz_admin ad   on ug.userid = ad.id
		left join gz_group g  on  g.id = ug.groupid
		where g.fname = '系统管理员组'  and  ad.username ='{$opername}' ";
		$ConData = SqlServerUtil::execToSql($this->SqlServer, $conSql );
		if(empty($ConData))
		{
			//不在 ，所在权限组 是否有权限查看
			$fieldSql ="SELECT  ad.username,g.fname ,fld.FFunc
			from gz_user_group ug
			left join  gz_admin ad   on ug.userid = ad.id
			left join gz_group g  on  g.id = ug.groupid
			left join gz_ObjectAccessField fld on fld.fgroupid = g.id
			where  ad.username ='{$opername}'";
			$fieldData = SqlServerUtil::execToSql($this->SqlServer, $fieldSql );
			$removeData = getFldViewDiff($fieldData);
		}
		//     	dd($removeData);
		//新增，修改 -- 显示 0 ，序时薄 ==0，1
		//FRptTypeFlag --序时薄显示0 ，1-隐藏
		//FUnControl -- 新增修改0 ，1-隐藏
		//FRepMeg -- 下拉框1 ,单选框2 ,多选框3 ,日期框4 ,禁止编辑5 , 多文本框6
		$FUnControlStr ='';
		$FUnControl  = $request->FUnControl??0;
		if ($FUnControl >= 0   ) {
			$FUnControlStr="and ( FUnControl =  {$FUnControl} )";
		}
		$basesql="select * from gz_RepListTitle where FRepKey='{$FRepKey}' and FRepPage = '{$FRepPage}' and FRepHide = 0  {$FUnControlStr}  {$opernameStr} order by FRepIndex ";
		$baseData = SqlServerUtil::execToSql($this->SqlServer, $basesql );
		if($baseData){
			$baseData = arraykeyToLower($baseData);
			foreach ($baseData as $k=>$v)
			{
				$baseData[$k]['frepfld'] = strtolower($v['frepfld']);
			}
		}
		// 过滤权限
		if($removeData){
			foreach ($baseData as $k1=> $v1)
			{
				
				if(in_array($v1['frightkey'] ,$removeData)) {
					unset($baseData[$k1]);
				}
			}
			$baseData = array_values($baseData);
			
		}
		//     	dd($baseData);
		 return $baseData ;
		
		}
		
		/**
		* 分析报表   新增数据 
		* @date: 2023年4月11日 下午4:16:08
		* @author: juyi
		* @param: variable
		* @return:
		*/
		function getfenxiBB($fbillno,$FRepKey, $FRepKeySC ){
			$DB = columnBiao($FRepKey) ;
			$sql2 = " select *  from {$DB} where fbillno  = '{$fbillno}' " ;
			$res1 = SqlServerUtil::exec1($this->SqlServer, strtolower($sql2) ,2 );
			$res2 =[];
			if($res1){
				 
				$res2['FStatus']  = 0;
				$res2['checkName']  = '';
				$res2['fbillno'] = $res1['fbillno']  ;
				$res2['fmodel'] = $res1['fmodel'] ;
				$res2['ftaxSinglezjcl'] = $res1['ftaxSinglezjcl'] ;
				$res2['fNotaxSinglezjcl'] = $res1['fNotaxSinglezjcl'] ;
				$res2['fhtpower'] = $res1['fhtpower'] ; 
				$res2['fjiazhangCB'] = $res1['fjiazhangCB'] ;
				$res2['fjiazhangcbJS'] = $res1['fjiazhangcbJS'] ;
				$res2['fjiazhangcbCG'] = $res1['fjiazhangcbCG'] ;
				$res2['fjiazhangcbQT'] = $res1['fjiazhangcbQT'] ;
				
				$res2['fcgcb'] = 0 ;
				$res2['fcgcbls'] = 0  ;
				
				$res2['fjzcgcb'] = 0  ;
				$res2['fjzcgcbls'] = 0  ;
				 
			}
			return $res2;
		}
		
		 
	}
	
	